package com.qf.android.huihui.adapter.shopping;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.qf.android.huihui.R;
import com.qf.android.huihui.bean.ShoppingBean;
import com.qf.android.huihui.common.IClickListern;

import java.util.List;

/**
 * Created by 韩爽 on 2017/1/9.
 */

public class ShoppoingTopAdapter extends PagerAdapter implements View.OnClickListener {
    private List<ShoppingBean.DataBean.TopBean> top;
    private Context context;
    private IClickListern listern=null;
    public ShoppoingTopAdapter(List<ShoppingBean.DataBean.TopBean> top, Context context) {
        this.top = top;
        this.context = context;
    }

    @Override
    public int getCount() {
        return top!=null?Integer.MAX_VALUE:0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %=top.size();
        View view= LayoutInflater.from(context).inflate(R.layout.fragment_recyclerview_head_top,container,false);
        ImageView  imageView= (ImageView) view.findViewById(R.id.fragment_recyclerview_head_top_image);
        view.setTag(top.get(position).getUrl());
        view.setOnClickListener(this);
        Glide.with(context).load(top.get(position).getImageUrl()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
        container.addView(view);
        return view;
    }
    public void setMyClickListener(IClickListern listener) {
        this.listern = listener;
    }

    @Override
    public void onClick(View v) {
        if(listern!=null){
           listern.OnItemClick(v,(String)v.getTag());
        }
    }
}
